package com.hna.iot.controller.sys;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hna.iot.base.BaseController;
import com.hna.iot.model.BfRole;
import com.hna.iot.model.BfUser;
import com.hna.iot.model.BfUser2role;
import com.hna.iot.service.IBfRoleService;
import com.hna.iot.service.IBfUser2roleService;



/**
 * 用户角色
 * @author Administrator
 *
 */
@Controller
public class UserRoleController extends BaseController{	
	
	private final static Logger log = LoggerFactory.getLogger(UserRoleController.class);
	@Autowired
	private IBfRoleService roleService;
	@Autowired
	private IBfUser2roleService user2RoleService;
	
	@RequestMapping("/userrole/edituserrole")
	public ModelAndView edituserrole(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("pages/userrole/edituserrole");
		mav.addObject("userId", request.getParameter("userId"));
		return mav;
	}
	
	
	@RequestMapping(value = { "/rest/userRoleList" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> userRoleList(HttpServletRequest request) {
		Map<String,Object> res = new HashMap<String,Object>();
		List<Map> rows = new ArrayList<Map>();
		BfUser userSession =(BfUser)request.getSession().getAttribute("userInfo");
		String username = userSession.getUsername();
		try {
			String userId = request.getParameter("userId");

			Wrapper<BfRole> wrapper=new EntityWrapper<BfRole>();
			if(username.equals("admin")){
				List<Long> idList = new ArrayList<Long>(Arrays.asList(9L,7L,8L));
				wrapper.in("id", idList);
			} 
			
			List<BfRole> roleList = roleService.selectList(wrapper);
			
			Wrapper<BfUser2role> wrapper2=new EntityWrapper<BfUser2role>();
			wrapper2.eq("userid", userId);
			List<BfUser2role> userRoleList = user2RoleService.selectList(wrapper2);
			
			for(BfRole r : roleList){
				Map<String,Object> row = new HashMap<String,Object>();
				row.put("id", r.getId());
				row.put("roledesc", r.getRoledesc());
				row.put("rolename", r.getRolename());
				boolean b = false;
				for(BfUser2role ur :userRoleList){
					if(r.getId().equals(ur.getRoleid())){
						b = true;
						break;
					}
				}
				row.put("checked", b);
				rows.add(row);
			}
			
			res.put("rows", rows);
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		} 	
	}
	
	@RequestMapping(value = { "/rest/userRoleEdit" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> userRoleEdit(HttpServletRequest request){
		Map<String,Object> res = new HashMap<String,Object>();
		try{
		BfUser userSession =(BfUser)request.getSession().getAttribute("userInfo");	
		String userId = request.getParameter("userId");
		
		String ids = request.getParameter("ids");

		Wrapper<BfUser2role> wrapper=new EntityWrapper<BfUser2role>();
		wrapper.eq("userid", userId);
		user2RoleService.delete(wrapper);
		
		if(StringUtils.isNotEmpty(ids)){
			String[] roleIds =ids.split(",");
			List<BfUser2role> user2RoleList = new ArrayList<BfUser2role>();
			for(String roleId : roleIds){
				BfUser2role user2Role = new BfUser2role();
				user2Role.setRoleid(Long.valueOf(roleId));
				user2Role.setUserid(Long.valueOf(userId));
				user2RoleList.add(user2Role);
			}
			user2RoleService.insertBatch(user2RoleList);
		}
		
		this.saveUserlog(request, "用户角色管理："+userSession.getUsername()+"修改用户角色操作");
		
		res.put("success", "true");
		return res;
		}catch(Exception e){
			res.put("success", "false");
			res.put("msg", e);
			return res;
		}
	}
	
}
